<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="设备序号" prop="deviceNo">
        <el-input
          v-model="queryParams.deviceNo"
          placeholder="请输入设备序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人脸序号" prop="facecode">
        <el-input
          v-model="queryParams.facecode"
          placeholder="请输入人脸序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="uuid">
        <el-input
          v-model="queryParams.uuid"
          placeholder="请输入用户id  (平台用户就对应uid 要是学生就对应id)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="识别时间" prop="faceTime">
        <el-date-picker clearable
          v-model="queryParams.faceTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择识别时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="位置" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="同步状态" prop="syn">
        <el-input
          v-model="queryParams.syn"
          placeholder="请输入同步状态 0 未同步 1 已经同步"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否推送" prop="isPush">
        <el-input
          v-model="queryParams.isPush"
          placeholder="请输入是否已经推送"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wisdom:record:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wisdom:record:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wisdom:record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wisdom:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="记录ID" align="center" prop="id" />
      <el-table-column label="设备序号" align="center" prop="deviceNo" />
      <el-table-column label="人脸序号" align="center" prop="facecode" />
      <el-table-column label="用户id" align="center" prop="uuid" />
      <el-table-column label="用户姓名" align="center" prop="name" />
      <el-table-column label="属性" align="center" prop="personType" />
      <el-table-column label="方向" align="center" prop="type" />
      <el-table-column label="识别时间" align="center" prop="faceTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.faceTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="位置" align="center" prop="location" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wisdom:record:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wisdom:record:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改进出记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="${comment}" prop="comId">
          <el-input v-model="form.comId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="设备序列号" prop="deviceNo">
          <el-input v-model="form.deviceNo" placeholder="请输入设备序列号" />
        </el-form-item>
        <el-form-item label="人脸序号" prop="facecode">
          <el-input v-model="form.facecode" placeholder="请输入人脸序号" />
        </el-form-item>
        <el-form-item label="用户id  (平台用户就对应uid 要是学生就对应id)" prop="uuid">
          <el-input v-model="form.uuid" placeholder="请输入用户id  (平台用户就对应uid 要是学生就对应id)" />
        </el-form-item>
        <el-form-item label="用户姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="识别时间" prop="faceTime">
          <el-date-picker clearable
            v-model="form.faceTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择识别时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="捕捉图" prop="picture">
          <el-input v-model="form.picture" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="同步状态 0 未同步 1 已经同步" prop="syn">
          <el-input v-model="form.syn" placeholder="请输入同步状态 0 未同步 1 已经同步" />
        </el-form-item>
        <el-form-item label="是否已经推送" prop="isPush">
          <el-input v-model="form.isPush" placeholder="请输入是否已经推送" />
        </el-form-item>
        <el-form-item label="推送次数 推送超过3次不再推送" prop="pushNum">
          <el-input v-model="form.pushNum" placeholder="请输入推送次数 推送超过3次不再推送" />
        </el-form-item>
        <el-form-item label="学生年级班级关联id" prop="relationId">
          <el-input v-model="form.relationId" placeholder="请输入学生年级班级关联id" />
        </el-form-item>
        <el-form-item label="温度" prop="temperature">
          <el-input v-model="form.temperature" placeholder="请输入温度" />
        </el-form-item>
        <el-form-item label="是否已经推送图片 0未推送 1已推送" prop="isPushPicture">
          <el-input v-model="form.isPushPicture" placeholder="请输入是否已经推送图片 0未推送 1已推送" />
        </el-form-item>
        <el-form-item label="楼宇ID" prop="buildingId">
          <el-input v-model="form.buildingId" placeholder="请输入楼宇ID" />
        </el-form-item>
        <el-form-item label="0 不带口罩 1带口罩" prop="mask">
          <el-input v-model="form.mask" placeholder="请输入0 不带口罩 1带口罩" />
        </el-form-item>
        <el-form-item label="相似度" prop="similar">
          <el-input v-model="form.similar" placeholder="请输入相似度" />
        </el-form-item>
        <el-form-item label="hash" prop="hash">
          <el-input v-model="form.hash" placeholder="请输入hash" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/wisdom/record";

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 进出记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        comId: null,
        deviceNo: null,
        facecode: null,
        uuid: null,
        name: null,
        personType: null,
        type: null,
        faceTime: null,
        picture: null,
        location: null,
        syn: null,
        isPush: null,
        pushNum: null,
        relationId: null,
        temperature: null,
        isPushPicture: null,
        buildingId: null,
        mask: null,
        similar: null,
        hash: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        comId: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        deviceNo: [
          { required: true, message: "设备序列号不能为空", trigger: "blur" }
        ],
        uuid: [
          { required: true, message: "用户id  (平台用户就对应uid 要是学生就对应id)不能为空", trigger: "blur" }
        ],
        personType: [
          { required: true, message: "1 教职业工  2学生  3家属 4其他人员 0是识别失败人员不能为空", trigger: "change" }
        ],
        type: [
          { required: true, message: "1 入  2出不能为空", trigger: "change" }
        ],
        syn: [
          { required: true, message: "同步状态 0 未同步 1 已经同步不能为空", trigger: "blur" }
        ],
        isPush: [
          { required: true, message: "是否已经推送不能为空", trigger: "blur" }
        ],
        relationId: [
          { required: true, message: "学生年级班级关联id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询进出记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        comId: null,
        deviceNo: null,
        facecode: null,
        uuid: null,
        name: null,
        personType: null,
        type: null,
        faceTime: null,
        picture: null,
        location: null,
        syn: null,
        isPush: null,
        pushNum: null,
        relationId: null,
        temperature: null,
        isPushPicture: null,
        buildingId: null,
        mask: null,
        similar: null,
        hash: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加进出记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改进出记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除进出记录编号为"' + ids + '"的数据项？').then(function() {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wisdom/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
